-
Notifications
You must be signed in to change notification settings - Fork 845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #883
base: master
Are you sure you want to change the base?
Solution #883
Conversation
taxi/models.py
Outdated
class VisitCounter(models.Model): | ||
num_visits = models.PositiveIntegerField(default=0) | ||
|
||
def increment(self): | ||
self.num_visits += 1 | ||
self.save() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need it here? Your counter should be on session
layer, so you shouldn't create this model
taxi/views.py
Outdated
def login_view(request): | ||
if request.method == "POST": | ||
form = AuthenticationForm(request, data=request.POST) | ||
if form.is_valid(): | ||
user = form.get_user() | ||
login(request, user) | ||
return redirect("taxi:index") | ||
else: | ||
form = AuthenticationForm() | ||
|
||
return render(request, "taxi/../templates/registration/login.html", | ||
{"form": form}) | ||
|
||
|
||
def logout_view(request): | ||
logout(request) | ||
return redirect("taxi:index") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create login/logout screens. Provide authentication to your project using built-in Django authentication.
Use bult-in authentication from django.contrib.auth.urls
, fix this
taxi/views.py
Outdated
if not request.user.is_authenticated: | ||
return redirect("taxi:login") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use @login_required
decorator
taxi/views.py
Outdated
if "num_visits" not in request.session: | ||
request.session["num_visits"] = 1 | ||
else: | ||
request.session["num_visits"] += 1 | ||
|
||
num_visits = request.session["num_visits"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor this using method request.session.get()
method
taxi/views.py
Outdated
@login_required | ||
def driver_detail(request, pk): | ||
driver = Driver.objects.get(pk=pk) | ||
return render(request, "taxi/driver_detail.html", {"driver": driver}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You already have DriverDetailView
with generic why did you create this?
taxi/views.py
Outdated
class ManufacturerListView(generic.ListView): | ||
model = Manufacturer | ||
context_object_name = "manufacturer_list" | ||
template_name = "taxi/manufacturer_list.html" | ||
paginate_by = 5 | ||
|
||
def get(self, request, *args, **kwargs): | ||
if not request.user.is_authenticated: | ||
return redirect("taxi:login") | ||
return super().get(request, *args, **kwargs) | ||
|
||
|
||
class CarListView(generic.ListView): | ||
model = Car | ||
paginate_by = 5 | ||
queryset = Car.objects.select_related("manufacturer") | ||
queryset = Car.objects.select_related("manufacturer").order_by("id") | ||
|
||
def get(self, request, *args, **kwargs): | ||
if not request.user.is_authenticated: | ||
return redirect("taxi:login") | ||
return super().get(request, *args, **kwargs) | ||
|
||
|
||
class CarDetailView(generic.DetailView): | ||
model = Car | ||
|
||
def get(self, request, *args, **kwargs): | ||
if not request.user.is_authenticated: | ||
return redirect("taxi:login") | ||
return super().get(request, *args, **kwargs) | ||
|
||
|
||
class DriverListView(generic.ListView): | ||
model = Driver | ||
paginate_by = 5 | ||
|
||
def get(self, request, *args, **kwargs): | ||
if not request.user.is_authenticated: | ||
return redirect("taxi:login") | ||
return super().get(request, *args, **kwargs) | ||
|
||
|
||
class DriverDetailView(generic.DetailView): | ||
model = Driver | ||
queryset = Driver.objects.prefetch_related("cars__manufacturer") | ||
|
||
def get(self, request, *args, **kwargs): | ||
if not request.user.is_authenticated: | ||
return redirect("taxi:login") | ||
return super().get(request, *args, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Read about LoginRequiredMixin
and generics. You don't need to override get
method here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left comments + in personal channel
path("login/", auth_views.LoginView.as_view(), name="login"), | ||
path("logout/", auth_views.LogoutView.as_view(), name="logout"), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to define this urls here. You should use already provided views from django auth.
You imported it in your settings app(main app).
No description provided.